package com.entity.implement;


import java.util.Date;

import com.entity.myInterface.TokenRepositoryCustom;
import com.okok.entity.User;
import com.shiro.entity.Token;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;


@Repository
public class TokenRepositoryImpl implements TokenRepositoryCustom {

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void unvalidUserTokens(User user, String op) {
		Query query = new Query();
		Criteria criteria = Criteria.where("user.$id").is( new ObjectId(user.getId()) );

		query.addCriteria( criteria );
		Update update = new Update();
		update.set("valid", false);
		update.set("modifyDate", new Date());
		update.set("modifier", op);
		mongoTemplate.updateMulti(query, update, Token.class);
	}

	@Override
	public Token findByToken(String token) {
		Query query = new Query();
		query.addCriteria( Criteria.where("token").is(token) );
		return mongoTemplate.findOne(query,Token.class);
	}

}